package com.atguigu.gulimall.ware.config;

import com.atguigu.common.constant.MqConstant;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MqConfig {

    //使用json序列化机制，进行消息转化
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

//    @RabbitListener(queues = MqConstant.WARE_RELEASE_QUEUE)
//    public void test(Message message, Channel channel){
//        System.out.println("消息的index-------------"+message.getMessageProperties().getDeliveryTag());
//    }

    //创建交换机
    @Bean
    public Exchange orderEventExchange(){
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange(MqConstant.WARE_EVENT_EXCHANGE,true,false,null);
    }

    //创建延时队列
    @Bean
    public Queue orderDelayQueue(){
        Map<String,Object> arguments=new HashMap<>();
        arguments.put("x-dead-letter-exchange", MqConstant.WARE_EVENT_EXCHANGE);  //死信后发送到的交换机
        arguments.put("x-dead-letter-routing-key",  MqConstant.WARE_RELEASE_ROUTING_KEY); // 死信队列的路由key
        arguments.put("x-message-ttl",  3*60*1000);   //延时时间
        /**
         *  String name   名称
         *  boolean durable  是否持久化
         *  boolean exclusive 是否排他   （不能多个通道channel同时使用)
         * boolean autoDelete  是否自动删除
         *  Map<String, Object> arguments  其它参数
         */
        Queue queue=new Queue(MqConstant.WARE_DELAY_QUEUE,true,false,false,arguments);
        return queue;
    }

    //创建死信队列
    @Bean
    public Queue orderReleaseOrderQueue(){
        Queue queue=new Queue(MqConstant.WARE_RELEASE_QUEUE,true,false,false,null);
        return queue;
    }

    //创建交换机与延时队列的绑定
    @Bean
    public Binding orderCreateOrderBinding(){
        /**
         *
         * String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
         *
         * String destination:  交换机需要绑定的目标
         * Binding.DestinationType  交换机绑定的目标类型
         * String exchang   需要绑定的交换机
         * String routingKey  绑定的路由
         * Map<String, Object> arguments  其它参数
         *
         */
        Binding binding=new Binding(MqConstant.WARE_DELAY_QUEUE, Binding.DestinationType.QUEUE,MqConstant.WARE_EVENT_EXCHANGE,MqConstant.WARE_DELAY_ROUTING_KEY,null);
        return binding;
    }

    //创建交换机与死信队列的绑定
    @Bean
    public Binding orderReleaseOtherBinding(){
        /**
         *
         * String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
         *
         * String destination:  交换机需要绑定的目标
         * Binding.DestinationType  交换机绑定的目标类型
         * String exchang   需要绑定的交换机
         * String routingKey  绑定的路由
         * Map<String, Object> arguments  其它参数
         *
         */
        Binding binding=new Binding(MqConstant.WARE_RELEASE_QUEUE, Binding.DestinationType.QUEUE,MqConstant.WARE_EVENT_EXCHANGE,MqConstant.WARE_RELEASE_ROUTING_KEY,null);
        return binding;
    }
}
